//
//  SelectHeadView.m
//  XDLBaseProject
//
//  Created by 谢东来 on 2019/9/5.
//  Copyright © 2019 Apple. All rights reserved.
//

#import "SelectHeadView.h"
#import "HeadButton.h"

@interface SelectHeadView()

@property (assign, nonatomic) double headImvWidth;
@property (strong, nonatomic) NSMutableArray *headArr;
@property (assign, nonatomic) NSInteger selectIndex;
@property (copy, nonatomic) CompleteBlock completeBlock;

@end

@implementation SelectHeadView

- (instancetype)init{
    if (self = [super init]) {
        self.backgroundColor = UIColorFromHexA(0x000000, 0.9);
        self.frame = CGRectMake(0, 0, kScreenW, kScreenH);
        self.selectIndex = [DataManager shareManager].dataModel.headIndex;
    }
    return self;
}

- (void)createSubviews {
    self.headImvWidth = (kScreenW - X_Real(20)*2 - X_Real(10)*3) / 4;
    
    UIView *headView = [UIView newAutoLayoutView];
    [self addSubview:headView];
    [headView autoSetDimensionsToSize:(CGSizeMake(kScreenW, self.headImvWidth*2 + Y_Real(10) + Y_Real(20)*2))];
    [headView autoAlignAxis:(ALAxisHorizontal) toSameAxisOfView:self withOffset:X_Real(-50)];
    [headView autoAlignAxisToSuperviewAxis:(ALAxisVertical)];
    
    UIButton *back = [UIButton newAutoLayoutView];
    [back setImage:[UIImage imageNamed:@"chahao_white.png"] forState:(UIControlStateNormal)];
    back.contentMode = UIViewContentModeScaleToFill;
    [back addTarget:self action:@selector(back) forControlEvents:(UIControlEventTouchUpInside)];
    [self addSubview:back];
    [back autoSetDimensionsToSize:(CGSizeMake(Width_Real(25), Width_Real(25)))];
    [back autoPinEdgeToSuperviewEdge:(ALEdgeTop) withInset: Y_Real(50)];
    [back autoPinEdgeToSuperviewEdge:(ALEdgeRight) withInset:X_Real(20)];
    
    UIButton *save = [UIButton newAutoLayoutView];
    [save setBackgroundImage:[UIImage imageWithColor:[AppColor getColor_MAIN] size:(CGSizeMake(Width_Real(100), Height_Real(40)))] forState:(UIControlStateNormal)];
    [save setTitle:@"保  存" forState:(UIControlStateNormal)];
    [save setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    [save setTitleColor:[UIColor grayColor] forState:(UIControlStateHighlighted)];
    save.titleLabel.font = [AppFont getFont_19];
    save.layer.cornerRadius = Width_Real(8);
    save.layer.borderWidth = Width_Real(1);
    save.layer.borderColor = [UIColor whiteColor].CGColor;
    save.layer.masksToBounds = YES;
    [save addTarget:self action:@selector(save) forControlEvents:(UIControlEventTouchUpInside)];
    [self addSubview:save];
    [save autoPinEdge:(ALEdgeTop) toEdge:(ALEdgeBottom) ofView:headView withOffset:X_Real(40)];
    [save autoSetDimensionsToSize:(CGSizeMake(Width_Real(100), Width_Real(40)))];
    [save autoAlignAxisToSuperviewAxis:(ALAxisVertical)];
    
    self.headArr = [NSMutableArray array];
    for(NSInteger i = 0; i < 8; i++) {
        HeadButton *head = [[HeadButton alloc] init];
        head.tag = 1000 + i;
        [head setImage:[UIImage imageNamed:[NSString stringWithFormat:@"head%ld.jpeg", i+1]] forState:(UIControlStateNormal)];
        head.imageView.contentMode = UIViewContentModeScaleToFill;
        head.frame = CGRectMake(X_Real(20)+(i%4)*(self.headImvWidth+X_Real(10)), Y_Real(20)+(i/4)*(self.headImvWidth+Y_Real(10)), self.headImvWidth, self.headImvWidth);
        head.layer.cornerRadius = self.headImvWidth/2;
        head.layer.masksToBounds = YES;
        [head addTarget:self action:@selector(selectHead:) forControlEvents:(UIControlEventTouchUpInside)];
        if (self.selectIndex == i) {
            head.selected = YES;
        }
        [headView addSubview:head];
        [self.headArr addObject:head];
    }
    
    [[UIApplication sharedApplication].delegate.window addSubview:self];
    [self exChangeOut:headView dur:0.2];
}

- (void)exChangeOut:(UIView *)changeOutView dur:(CFTimeInterval)dur{
    CAKeyframeAnimation * animation;
    animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    animation.duration = dur;
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;
    NSMutableArray *values = [NSMutableArray array];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.5, 0.5, 0.5)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
    animation.values = values;
    animation.timingFunction = [CAMediaTimingFunction functionWithName: @"easeInEaseOut"];
    [changeOutView.layer addAnimation:animation forKey:nil];
}

- (void)selectHead:(HeadButton *)sender {
    sender.selected = YES;
    if (sender.tag - 1000 != self.selectIndex) {
        [(HeadButton *)self.headArr[self.selectIndex] setSelected:NO];
        self.selectIndex = sender.tag - 1000;
    }
}

- (void)back {
    [self removeFromSuperview];
}

- (void)save {
    [DataManager shareManager].dataModel.headIndex = self.selectIndex;
    [[DataManager shareManager] saveData];
    self.completeBlock();
    [self removeFromSuperview];
}

- (void)show:(CompleteBlock)completeBlock {
    self.completeBlock = completeBlock;
    [self createSubviews];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
